<template>
  <div class="consumable-container">
    <div class="page-header">
      <button class="back-button" @click="goToHome">
        <span class="icon-arrow-left"></span>
        返回首页
      </button>
      <h1>耗材列表管理</h1>
    </div>
    
    <div class="page-content">
      <!-- 操作工具栏 -->
      <div class="toolbar">
        <div class="search-filter">
          <input 
            type="text" 
            v-model="searchKeyword" 
            placeholder="搜索耗材名称或规格..."
            @input="handleSearch"
          >
          <select v-model="selectedCategory" @change="handleSearch">
            <option value="">所有分类</option>
            <option v-for="category in categories" :key="category.id" :value="category.name">
              {{ category.name }}
            </option>
          </select>
        </div>
        
        <button class="add-btn" @click="showAddForm = true">
          <span class="icon-plus">+</span> 添加耗材
        </button>
      </div>
      
      <!-- 耗材列表表格 -->
      <div class="table-container">
        <table class="consumable-table">
          <thead>
            <tr>
              <th>ID</th>
              <th>耗材名称</th>
              <th>分类</th>
              <th>规格型号</th>
              <th>单位</th>
              <th>当前库存</th>
              <th>库存预警线</th>
              <th>状态</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="item in filteredConsumables" :key="item.id">
              <td>{{ item.id }}</td>
              <td>{{ item.name }}</td>
              <td>{{ item.category }}</td>
              <td>{{ item.specification }}</td>
              <td>{{ item.unit }}</td>
              <td :class="{ 'low-stock': item.stock <= item.warningLine }">
                {{ item.stock }}
              </td>
              <td>{{ item.warningLine }}</td>
              <td>
                <span class="status-tag" :class="item.status === '正常' ? 'status-normal' : 'status-disabled'">
                  {{ item.status }}
                </span>
              </td>
              <td class="action-buttons">
                <button class="edit-btn" @click="editConsumable(item)">编辑</button>
                <button class="delete-btn" @click="deleteConsumable(item.id)">删除</button>
              </td>
            </tr>
            <tr v-if="filteredConsumables.length === 0">
              <td colspan="9" class="no-data">暂无耗材数据</td>
            </tr>
          </tbody>
        </table>
      </div>
      
      <!-- 分页控件 -->
      <div class="pagination">
        <button :disabled="currentPage === 1" @click="currentPage--">上一页</button>
        <span>{{ currentPage }} / {{ totalPages }}</span>
        <button :disabled="currentPage === totalPages" @click="currentPage++">下一页</button>
      </div>
      
      <!-- 添加/编辑耗材弹窗 -->
      <div class="modal-mask" v-if="showAddForm || showEditForm">
        <div class="modal-container">
          <div class="modal-header">
            <h3>{{ showAddForm ? '添加耗材' : '编辑耗材' }}</h3>
            <button class="close-btn" @click="closeModal">×</button>
          </div>
          
          <div class="modal-body">
            <form class="consumable-form">
              <div class="form-group">
                <label>耗材名称 <span class="required">*</span></label>
                <input type="text" v-model="formData.name" placeholder="请输入耗材名称">
              </div>
              
              <div class="form-group">
                <label>分类 <span class="required">*</span></label>
                <select v-model="formData.category">
                  <option value="">请选择分类</option>
                  <option v-for="category in categories" :key="category.id" :value="category.name">
                    {{ category.name }}
                  </option>
                </select>
              </div>
              
              <div class="form-row">
                <div class="form-group">
                  <label>规格型号</label>
                  <input type="text" v-model="formData.specification" placeholder="请输入规格型号">
                </div>
                <div class="form-group">
                  <label>单位 <span class="required">*</span></label>
                  <input type="text" v-model="formData.unit" placeholder="如：个、箱、卷">
                </div>
              </div>
              
              <div class="form-row">
                <div class="form-group">
                  <label>当前库存</label>
                  <input type="number" v-model="formData.stock" min="0">
                </div>
                <div class="form-group">
                  <label>库存预警线</label>
                  <input type="number" v-model="formData.warningLine" min="0">
                </div>
              </div>
              
              <div class="form-group">
                <label>状态</label>
                <select v-model="formData.status">
                  <option value="正常">正常</option>
                  <option value="停用">停用</option>
                </select>
              </div>
              
              <div class="form-group">
                <label>备注</label>
                <textarea v-model="formData.remarks" rows="3" placeholder="请输入备注信息"></textarea>
              </div>
            </form>
          </div>
          
          <div class="modal-footer">
            <button class="cancel-btn" @click="closeModal">取消</button>
            <button class="confirm-btn" @click="submitForm">确认</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ConsumableList',
  data() {
    return {
      // 耗材数据
      consumables: [
        {
          id: 1,
          name: '打印机墨盒',
          category: '办公设备',
          specification: 'HP-12A',
          unit: '个',
          stock: 15,
          warningLine: 5,
          status: '正常',
          remarks: '适用于HP LaserJet系列打印机'
        },
        {
          id: 2,
          name: '复印纸',
          category: '办公用纸',
          specification: 'A4-70g',
          unit: '箱',
          stock: 30,
          warningLine: 10,
          status: '正常',
          remarks: '每箱5包，每包500张'
        },
        {
          id: 3,
          name: 'U盘',
          category: '存储设备',
          specification: '32GB',
          unit: '个',
          stock: 8,
          warningLine: 5,
          status: '正常',
          remarks: 'USB3.0接口'
        },
        {
          id: 4,
          name: '鼠标',
          category: '电脑配件',
          specification: '无线',
          unit: '个',
          stock: 3,
          warningLine: 5,
          status: '正常',
          remarks: '蓝牙连接'
        },
        {
          id: 5,
          name: '键盘',
          category: '电脑配件',
          specification: '机械',
          unit: '个',
          stock: 0,
          warningLine: 3,
          status: '停用',
          remarks: '青轴'
        }
      ],
      
      // 分类数据
      categories: [
        { id: 1, name: '办公设备' },
        { id: 2, name: '办公用纸' },
        { id: 3, name: '存储设备' },
        { id: 4, name: '电脑配件' },
        { id: 5, name: '网络设备' }
      ],
      
      // 搜索和筛选
      searchKeyword: '',
      selectedCategory: '',
      currentPage: 1,
      pageSize: 10,
      
      // 弹窗控制
      showAddForm: false,
      showEditForm: false,
      currentEditId: null,
      
      // 表单数据
      formData: {
        name: '',
        category: '',
        specification: '',
        unit: '',
        stock: 0,
        warningLine: 0,
        status: '正常',
        remarks: ''
      }
    };
  },
  computed: {
    filteredConsumables() {
      return this.consumables
        .filter(item => 
          (this.searchKeyword === '' || 
           item.name.includes(this.searchKeyword) || 
           item.specification.includes(this.searchKeyword)) &&
          (this.selectedCategory === '' || item.category === this.selectedCategory)
        )
        .slice(
          (this.currentPage - 1) * this.pageSize,
          this.currentPage * this.pageSize
        );
    },
    totalPages() {
      const filteredCount = this.consumables.filter(item => 
        (this.searchKeyword === '' || 
         item.name.includes(this.searchKeyword) || 
         item.specification.includes(this.searchKeyword)) &&
        (this.selectedCategory === '' || item.category === this.selectedCategory)
      ).length;
      
      return Math.ceil(filteredCount / this.pageSize);
    }
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    
    handleSearch() {
      this.currentPage = 1; // 搜索时重置到第一页
    },
    
    editConsumable(item) {
      this.currentEditId = item.id;
      this.formData = { ...item };
      this.showEditForm = true;
    },
    
    deleteConsumable(id) {
      if (confirm('确定要删除该耗材吗？')) {
        this.consumables = this.consumables.filter(item => item.id !== id);
      }
    },
    
    closeModal() {
      this.showAddForm = false;
      this.showEditForm = false;
      this.currentEditId = null;
      this.resetForm();
    },
    
    resetForm() {
      this.formData = {
        name: '',
        category: '',
        specification: '',
        unit: '',
        stock: 0,
        warningLine: 0,
        status: '正常',
        remarks: ''
      };
    },
    
    submitForm() {
      // 简单验证
      if (!this.formData.name || !this.formData.category || !this.formData.unit) {
        alert('请填写必填字段');
        return;
      }
      
      if (this.showAddForm) {
        // 添加新耗材
        const newId = Math.max(...this.consumables.map(item => item.id), 0) + 1;
        this.consumables.push({
          ...this.formData,
          id: newId
        });
        alert('耗材添加成功');
      } else if (this.showEditForm) {
        // 编辑现有耗材
        const index = this.consumables.findIndex(item => item.id === this.currentEditId);
        if (index !== -1) {
          this.consumables[index] = { ...this.formData, id: this.currentEditId };
          alert('耗材编辑成功');
        }
      }
      
      this.closeModal();
    }
  }
};
</script>

<style scoped>
.consumable-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e1e5e8;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

.icon-arrow-left::before {
  content: "←";
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 15px;
}

.search-filter {
  display: flex;
  gap: 10px;
  align-items: center;
}

.search-filter input,
.search-filter select {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
}

.search-filter input {
  width: 300px;
}

.add-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 8px 15px;
  background: #2ecc71;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  font-weight: 500;
}

.add-btn:hover {
  background: #27ae60;
  transform: translateY(-2px);
}

.icon-plus {
  font-weight: bold;
}

.table-container {
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.consumable-table {
  width: 100%;
  border-collapse: collapse;
}

.consumable-table th,
.consumable-table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #f0f0f0;
}

.consumable-table th {
  background-color: #f9fafb;
  font-weight: 600;
  color: #333;
}

.consumable-table tr:hover {
  background-color: #f9fafb;
}

.low-stock {
  color: #e74c3c;
  font-weight: bold;
}

.status-tag {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.status-normal {
  background-color: #eafaf1;
  color: #27ae60;
}

.status-disabled {
  background-color: #fdedeb;
  color: #e74c3c;
}

.action-buttons {
  display: flex;
  gap: 5px;
}

.edit-btn {
  padding: 4px 8px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 3px;
  cursor: pointer;
  transition: background 0.3s;
  font-size: 13px;
}

.edit-btn:hover {
  background: #2980b9;
}

.delete-btn {
  padding: 4px 8px;
  background: #e74c3c;
  color: white;
  border: none;
  border-radius: 3px;
  cursor: pointer;
  transition: background 0.3s;
  font-size: 13px;
}

.delete-btn:hover {
  background: #c0392b;
}

.no-data {
  text-align: center;
  color: #999;
  padding: 40px;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15px;
  margin-top: 20px;
  padding: 10px;
}

.pagination button {
  padding: 5px 12px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.pagination button:hover:not(:disabled) {
  background: #f5f5f5;
  border-color: #ccc;
}

.pagination button:disabled {
  color: #999;
  cursor: not-allowed;
}

/* 弹窗样式 */
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-container {
  background-color: white;
  border-radius: 10px;
  width: 500px;
  max-width: 90%;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  animation: modalFadeIn 0.3s;
}

@keyframes modalFadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.modal-header {
  padding: 15px 20px;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #999;
  transition: color 0.3s;
}

.close-btn:hover {
  color: #333;
}

.modal-body {
  padding: 20px;
}

.consumable-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.form-row {
  display: flex;
  gap: 15px;
}

.form-row .form-group {
  flex: 1;
}

.form-group label {
  font-size: 14px;
  color: #666;
}

.required {
  color: #e74c3c;
}

.form-group input,
.form-group select,
.form-group textarea {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  border-color: #3498db;
  outline: none;
}

.modal-footer {
  padding: 15px 20px;
  border-top: 1px solid #f0f0f0;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.cancel-btn {
  padding: 8px 15px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
}

.cancel-btn:hover {
  background: #f5f5f5;
}

.confirm-btn {
  padding: 8px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.confirm-btn:hover {
  background: #2980b9;
}
</style>